package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.JwLeaveInfoBo;
import org.dromara.system.domain.vo.JwLeaveInfoVo;
import org.dromara.system.domain.JwLeaveInfo;
import org.dromara.system.mapper.JwLeaveInfoMapper;
import org.dromara.system.service.IJwLeaveInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 请假信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@RequiredArgsConstructor
@Service
public class JwLeaveInfoServiceImpl implements IJwLeaveInfoService {

    private final JwLeaveInfoMapper baseMapper;

    /**
     * 查询请假信息
     */
    @Override
    public JwLeaveInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询请假信息列表
     */
    @Override
    public TableDataInfo<JwLeaveInfoVo> queryPageList(JwLeaveInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JwLeaveInfo> lqw = buildQueryWrapper(bo);
        Page<JwLeaveInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询请假信息列表
     */
    @Override
    public List<JwLeaveInfoVo> queryList(JwLeaveInfoBo bo) {
        LambdaQueryWrapper<JwLeaveInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JwLeaveInfo> buildQueryWrapper(JwLeaveInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JwLeaveInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), JwLeaveInfo::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), JwLeaveInfo::getType, bo.getType());
        lqw.eq(bo.getDate() != null, JwLeaveInfo::getDate, bo.getDate());
        lqw.eq(bo.getStartTime() != null, JwLeaveInfo::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, JwLeaveInfo::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherId()), JwLeaveInfo::getTeacherId, bo.getTeacherId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), JwLeaveInfo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增请假信息
     */
    @Override
    public Boolean insertByBo(JwLeaveInfoBo bo) {
        JwLeaveInfo add = MapstructUtils.convert(bo, JwLeaveInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改请假信息
     */
    @Override
    public Boolean updateByBo(JwLeaveInfoBo bo) {
        JwLeaveInfo update = MapstructUtils.convert(bo, JwLeaveInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwLeaveInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除请假信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
